package weeek11.topic06;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.Jedis;

import java.util.Arrays;
import java.util.Set;

/**
 * @author lfy
 */
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class RedisTemplateTest {
    public static void main(String[] args) {
        ConfigurableApplicationContext ctx = new SpringApplicationBuilder()
                .sources(RedisTemplateTest.class).web(WebApplicationType.NONE).run(args);
        RedisTemplate<String, String> redisTemplate = (RedisTemplate<String, String>) ctx.getBean("redisTemplate");
        //org.springframework.data.redis.core.RedisTemplate
        System.out.println(redisTemplate.getClass());
        //String类型的操作
        redisTemplate.opsForValue().set("key", "value");
        String value = redisTemplate.opsForValue().get("key");
        System.out.println("String read:" + value);

        //list类型的操作
        redisTemplate.opsForList().leftPush("list_key", "list_v1");
        Long len = redisTemplate.opsForList().leftPush("list_key","list_v2");
        while (redisTemplate.opsForList().size("list_key")> 0) {
            String val = redisTemplate.opsForList().rightPop("list_key");
            System.out.println("list read:" + val);
        }

        //zset 类型操作
        redisTemplate.opsForZSet().add("zset_key", "zset_1", 1);
        redisTemplate.opsForZSet().add("zset_key", "zset_2", 2);
        redisTemplate.opsForZSet().add("zset_key", "zset_3", 3);

        Set<String> zset = redisTemplate.opsForZSet().range("zset_key", 0, 5);
        System.out.println(zset);


    }


}
